require('../css/courseInfo.less')
const baseURL = require('../utils/baseURL.js')


window.onload = function () {
    // 返回上一个页面
    let back = document.querySelector('.back')
    back.addEventListener('click', function () {
        location.href = 'motion.html'
    })
    let id = location.search.slice(1).split('=')[1]
    let fragments = []
    let title = document.querySelector('.title')
    let img = document.querySelectorAll('img')
    let span = document.querySelectorAll('span')
    // 请求课程的详细信息
    axios.get('/api/train/courseDetail?id=' + id).then(function (res) {
        let data = res.data.data;
        title.innerText = data.name;
        img[0].src = baseURL + data.imgurl;
        span[0].innerHTML = data.calorie
        span[1].innerHTML = data.time
        span[2].innerHTML = data.peoplenum

        // 将课程中视频片段赋值给全局变量
        fragments = data.fragments;
        console.log(fragments);

    })
    // 请求个人的信息
    let user = document.querySelector('.n-user')
    let sign = document.querySelector('.n-sign')
    axios.get('/api/user/info').then(function (res) {
        let data = res.data.data
        img[2].src = baseURL + data.imgUrl
        user.innerHTML = data.nickName
        sign.innerHTML = data.sign
    })

    // 跳转到新的页面
    let btn = document.querySelector('.btn')
    console.log(btn);
    img[1].addEventListener('click', function () {
        btn.click()
    })
    btn.addEventListener('click', function () {
        sessionStorage.setItem('fragments', JSON.stringify(fragments))
        location.href = 'play.html'
    })
}